<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Redis;

class SystemConfig extends Model
{
    use HasFactory;

    protected $table = 'system_config';

    /**
     * 是否主动维护时间戳
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * 查询配置值
     * @param $key
     * @param string $default
     * @return mixed|string
     */
    public static function findValue($key, $default = '')
    {
        $result = Redis::get($key);
        if (isset($result)) {
            return $result;
        }

        $result = self::where('key', $key)->value('value');
        if (!empty($result)) {
            Redis::set($key, $result);
            return $result;
        }
        return $default;
    }
}
